"""
数据源字段相关的Pydantic模式
"""
from typing import Optional, List
from pydantic import BaseModel
from datetime import datetime


class DataSourceFieldBase(BaseModel):
    """数据源字段基础模式"""
    name: str
    display_name: Optional[str] = None
    field_type: str
    description: Optional[str] = None
    is_primary: bool = False
    is_nullable: bool = True
    is_email: bool = False
    is_name: bool = False
    sample_value: Optional[str] = None
    data_format: Optional[str] = None


class DataSourceFieldCreate(DataSourceFieldBase):
    """创建数据源字段模式"""
    pass


class DataSourceFieldUpdate(BaseModel):
    """更新数据源字段模式"""
    display_name: Optional[str] = None
    description: Optional[str] = None
    is_email: Optional[bool] = None
    is_name: Optional[bool] = None
    sample_value: Optional[str] = None
    data_format: Optional[str] = None


class DataSourceField(DataSourceFieldBase):
    """数据源字段响应模式"""
    id: int
    data_source_id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class DataSourceFieldAnalysis(BaseModel):
    """数据源字段分析结果"""
    fields: List[DataSourceField]
    total_fields: int
    email_fields: List[str]
    name_fields: List[str]
    primary_fields: List[str]
    sample_data: Optional[dict] = None


class DataSourceDataPreview(BaseModel):
    """数据源数据预览"""
    fields: List[DataSourceField]
    data: List[dict]
    total_rows: int
    sample_size: int


class FieldMapping(BaseModel):
    """字段映射"""
    source_field: str
    target_field: str
    transform_rule: Optional[str] = None


class DataSourceQuery(BaseModel):
    """数据源查询参数"""
    data_source_id: int
    table_name: Optional[str] = None
    fields: Optional[List[str]] = None
    filters: Optional[dict] = None
    limit: Optional[int] = 100
    offset: Optional[int] = 0
    order_by: Optional[str] = None
